package com.qf.agro.service.crop.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.mapper.crop.CropFactoryMapper;
import com.qf.agro.pojo.crop.CropFactory;
import com.qf.agro.service.crop.CropFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CropFactoryServiceImpl implements CropFactoryService {

    @Autowired
    private CropFactoryMapper cropFactoryMapper;

    @Override
    public Page<CropFactory> findPage(Integer pageNum, Integer pageSize, String name) {
        Page<CropFactory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CropFactory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, CropFactory::getName, name);
        page = cropFactoryMapper.selectPage(page, queryWrapper);
        return page;

    }

    @Override
    public void save(CropFactory cropFactory) {
        cropFactory.setCreateTime(new Date());
        cropFactory.setState(1);
        cropFactoryMapper.insert(cropFactory);
    }

    @Override
    public void disabled(Long id) {
        CropFactory cropFactory = cropFactoryMapper.selectById(id);
        if (cropFactory.getState() == 1) {
            cropFactory.setState(0);
        } else {
            cropFactory.setState(1);
        }
        LambdaQueryWrapper<CropFactory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CropFactory::getId, id);
        cropFactoryMapper.update(cropFactory, queryWrapper);
    }

    @Override
    public void update(CropFactory cropFactory) {
        cropFactoryMapper.updateById(cropFactory);
    }
}
